/*
 * Copyright (c) 2020 - 2021 IoT Company of Midea Group.
 *
 * File Name 	    : ms_hal_flash.h
 * Description	    : HAL adaptor
 *
 * Version	        : v0.2
 * Author			: 
 * Date	            : 2020/12/28
 * History	        : 
 */

#ifndef __MS_HAL_FLASH_H__
#define __MS_HAL_FLASH_H__

/**
 * !!! please see the config.mk to make sure your chip whether had those function !!!
 * MS_SST
 * !!! if not, do not adapter following interfaces
 */

#include "ms_osal.h"
#include "ms_hal.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief erase flash-kv component data
 * @param name[in] : kv name
 */
void ms_hal_flash_msmart_cfg_info_erase(const char *name);

/**
 * @brief read flash-kv component data
 * @param name[in] : kv name
 * @param buf[out] out_buffer pointer, the read data stored in out_buffer
 * @param len[in] out_buffer max length,
 * @return if  successly, return MS_HAL_RESULT_SUCCESS
 */
ms_hal_result_t ms_hal_flash_msmart_cfg_info_read(const char *name, uint8_t *buf, unsigned int len);

/**
 * @brief write flash-kv component data
 * @param name[in] : kv name
 * @param buf[in] in_buffer pointer, the write data stored in in_buffer
 * @param len[in] in_buffer max length,
 * @return if  successly, return MS_HAL_RESULT_SUCCESS
 */
ms_hal_result_t ms_hal_flash_msmart_cfg_info_save(const char *name, uint8_t *buf, unsigned int len);

#if defined(MS_SST)

/**
 * @brief erase flash midea sst operate license
 */
void ms_hal_flash_license_operation_info_erase(void);

/**
 * @brief read flash midea sst operate license
 * @param buf[out] : sst operate lincese data
 * @param len[in] : length of sst lincese data
 * @return if  successly, return MS_HAL_RESULT_SUCCESS
 *         else return MS_HAL_RESULT_ERROR
 */
ms_hal_result_t ms_hal_flash_license_operation_info_read(uint8_t *buf, unsigned int len);

/**
 * @brief write flash midea sst license
 * @param buf[in] : sst operate lincese data
 * @param len[in] : length of sst lincese data
 * @return if  successly, return MS_HAL_RESULT_SUCCESS
 *         else return MS_HAL_RESULT_ERROR
 */
ms_hal_result_t ms_hal_flash_license_operation_info_write(uint8_t *buf, unsigned int len);

/**
 * @brief read flash midea sst license, which had write in factory settings.
 * @param buf[out] : sst lincese data
 * @param len[in] : length of sst lincese data
 * @return if  successly, return MS_HAL_RESULT_SUCCESS
 *         else return MS_HAL_RESULT_ERROR
 */
ms_hal_result_t ms_hal_flash_license_bakup_info_read(uint8_t *buf, unsigned int len);

#endif

/**
 * @brief init hal flash
 */
ms_hal_result_t ms_hal_flash_init(void);

/**
 * @brief erase flash data
 */
ms_hal_result_t ms_hal_flash_partition_erase(ms_hal_partition_t ms_ota_partition, uint32_t off_set,uint32_t len);

/**
 * @brief read flash partition data
 * @param ms_ota_partition[in] : please see ms_hal_partition_t
 * @param off_set[in] : address offset
 * @param buf[out] : flash data
 * @param len[in] : length of buffer
 * @return if  successly, return MS_HAL_RESULT_SUCCESS
 *         else return MS_HAL_RESULT_ERROR
 */
ms_hal_result_t ms_hal_flash_partition_read(ms_hal_partition_t ms_ota_partition, uint32_t *off_set, uint8_t *buf, unsigned int len);

/**
 * @brief write flash partition data
 * @param ms_ota_partition[in] : please see ms_hal_partition_t
 * @param off_set[in] : address offset
 * @param buf[in] : flash data
 * @param len[in] : length of buffer
 * @return if  successly, return MS_HAL_RESULT_SUCCESS
 *         else return MS_HAL_RESULT_ERROR
 */
ms_hal_result_t ms_hal_flash_partition_write(ms_hal_partition_t ms_ota_partition, uint32_t *off_set,uint8_t *buf, uint32_t len);

/**
 * @brief read flash partition information
 * @param ms_ota_partition[in] : please see ms_hal_partition_t
 * @param info[in] : please see ms_hal_logic_partition_t
 * @return if  successly, return MS_HAL_RESULT_SUCCESS
 *         else return MS_HAL_RESULT_ERROR
 */
ms_hal_result_t ms_hal_flash_partition_get_info(ms_hal_partition_t ms_ota_partition, ms_hal_logic_partition_t *info);

#ifdef __cplusplus
}
#endif

#endif

